//
//  DetailBarnerView.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/1.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "DetailBarnerView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "ACMacros.h"

@implementation DetailBarnerView {
    NSInteger pageCount;
    CGFloat bannerWidth;
    CGFloat bannerHeight;
    
    NSMutableArray *imagesArray;
    NSMutableArray *imageDataUrls;
    
    NSTimer *timer;
    CGFloat currentPage;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    [self.room_A_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.room_B_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.room_C_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [self.whole_Room_Button.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    
    [self.room_A_Button addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    [self.room_B_Button addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    [self.room_C_Button addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    [self.whole_Room_Button addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
    
    self.mainScrollView.delegate = self;
    
    [self.room_A_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.room_B_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.room_C_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.whole_Room_Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.room_A_Button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
    [self.room_B_Button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
    [self.room_C_Button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
    [self.whole_Room_Button setTitleColor:RGBACOLOR(255, 118, 108, 1) forState:UIControlStateSelected];
}

- (IBAction)room_A_ButtonClicked:(UIButton *)sender {
    [self roomButtonClicked:sender];
    [self.delegate buttonClicked:sender];
}
- (IBAction)room_B_ButtonClicked:(UIButton *)sender {
    [self roomButtonClicked:sender];
    [self.delegate buttonClicked:sender];
}

- (IBAction)room_C_ButtonClicked:(UIButton *)sender {
    [self roomButtonClicked:sender];
    [self.delegate buttonClicked:sender];
}

- (IBAction)whole_Room_ButtonClicked:(UIButton *)sender {
    [self roomButtonClicked:sender];
    [self.delegate buttonClicked:sender];
}

- (IBAction)hallButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)kitchenButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)tolietButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)balconyButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)communityButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}
- (IBAction)LikeButtonClicked:(UIButton *)sender {
    [self.delegate buttonClicked:sender];
}

- (void)roomButtonClicked:(UIButton *)sender {
    if (!sender.isSelected) {
        [sender setSelected:true];
        NSArray *buttonArray = @[self.room_A_Button, self.room_B_Button, self.room_C_Button, self.whole_Room_Button];
        for (UIButton *button in buttonArray) {
            if (button != sender && button.isSelected) {
                [button setSelected:false];
            }
        }
    }
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    UIButton * button = (UIButton* )object;
    
    if([keyPath isEqualToString:@"selected"])
    {
        if(!button.selected) {
            button.layer.borderColor = [[UIColor whiteColor] CGColor];
        } else{
            button.layer.borderColor = [RGBACOLOR(255, 118, 108, 1) CGColor];
        }
    }
}

- (void)refreshBannerWithImageUrls:(NSArray *)urls {
    imagesArray = [NSMutableArray arrayWithArray:urls];
    [self refreshBannerWithImages];
}

- (void)refreshBannerWithImages {
    for (id item in self.mainScrollView.subviews) {
        if ([item class] == [UIImageView class] || [item class] == [UIWebView class]) {
            [item removeFromSuperview];
        }
    }
    if (imagesArray.count == 0) {
        [imagesArray addObject:@"http://img.fangmm.com/default.jpg"];
    }
    NSInteger count = imagesArray.count;
    [self.mainScrollView setContentSize:CGSizeMake(count * Main_Screen_Width, 0)];
    [self.pageLabel setText:[NSString stringWithFormat:@"%d/%ld",1,(long)count]];
    if (count == 0) {
        [self.mainScrollView setContentOffset:CGPointMake(0, 0)];
        //TestLog
        NSLog(@"banner dataArray is Nil");
    } else {
        
        for (int i = 0; i < count; i++) {
            UIImageView *bannerPageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * Main_Screen_Width, 0, Main_Screen_Width, 307)];
            [self.mainScrollView addSubview:bannerPageView];
            [bannerPageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?q=60",imagesArray[i]]]placeholderImage:[UIImage imageNamed:@"photo_non"]];
            bannerPageView.contentMode = UIViewContentModeScaleToFill;
            currentPage = 0;
            //            [self startAutoPaging];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    CGFloat width = scrollView.contentSize.width;
    int curx = ceil(offsetX);
    int tempBannerWidth = ceil(Main_Screen_Width);
    int page = ceil(curx/tempBannerWidth + 1);
    int totalPage = ceil(width/tempBannerWidth);
    [self.pageLabel setText:[NSString stringWithFormat:@"%d/%d",page,totalPage]];
}

@end
